package com.six.controller;

import com.six.domain.entity.Dish;
import com.six.service.DishService;
import com.six.util.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/dish")
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    /* 新增菜品：storeId 由前端传，id 自增 */
    @PostMapping
    public Result<Void> insertDish(@RequestBody Dish dish,
                                   @RequestParam long storeId) {
        dish.setStoreId(storeId);   // 把门店号写进实体
        dishService.save(dish);
        return Result.success();
    }

    /* 修改菜品：storeId 作为额外参数，用于门店隔离 */
    @PutMapping
    public Result<Void> updateDish(@RequestBody Dish dish,
                                   @RequestParam long storeId) {
        dish.setStoreId(storeId);
        dishService.update(dish);
        return Result.success();
    }

    /* 批量删除：门店 + id 列表 */
    @DeleteMapping("/batch")
    public Result<Void> deleteBatch(@RequestBody List<Long> ids,
                                    @RequestParam Integer storeId) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("id 列表不能为空");
        }
        dishService.deleteBatch(storeId, ids);
        return Result.success();
    }

    /* 模糊查询：门店 + 关键字 */
    @GetMapping("/search")
    public Result<List<Dish>> searchByName(@RequestParam String name,
                                           @RequestParam Integer storeId) {
        List<Dish> list = dishService.searchByName(storeId, name);
        log.info("门店 {} 查询结果条数：{}", storeId, list.size());
        return Result.success(list);
    }
}